package com.swgk.core.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.swgk.core.R;
import com.swgk.core.base.model.entity.SelectPopEntity;
import com.swgk.core.viewpager.SelectButtonAdapter;

import java.util.List;


public class ScreenChildTextView extends RelativeLayout {

    private Context context;
    private SelectButtonAdapter adapter;
    private RecyclerViewAtViewPager2 recyclerView;
    private ICallPop iCallPop;
    private TextView tvLine;


    public ScreenChildTextView(Context context) {
        this(context, null);
    }

    public ScreenChildTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScreenChildTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
        perseAttrs(attrs);
    }

    private void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.view_select_button_p, this);
        recyclerView = view.findViewById(R.id.rv_button);
        tvLine = view.findViewById(R.id.tv_line);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        adapter = new SelectButtonAdapter(context, null);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        adapter.setOnButtonClick(new SelectButtonAdapter.IButtonClick() {
            @Override
            public void clickButton(LinearLayout bg, ImageView imageView, TextView content, int position, SelectPopEntity entity) {
                iCallPop.openPop(position, content, bg, entity);
            }
        });

        adapter.setOnOpen(isOpen -> {
            if (isOpen) {
                tvLine.setBackgroundColor(context.getResources().getColor(R.color.color_f5f5f5));
            } else {
                tvLine.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
        });
    }


    public void setTextButtonList(List<SelectPopEntity> list) {
        adapter.setDatas(list);
    }

    public void addTextButtonList(List<SelectPopEntity> list) {
        adapter.addDatas(list);
    }

    public void addTextButtonList(List<SelectPopEntity> list, int positon) {
        adapter.addDatas(list, positon);
    }


    public void addTextButton(SelectPopEntity selectPopEntity) {
        if (!adapter.getDatas().contains(selectPopEntity)) {
            adapter.addData(selectPopEntity);
        }
    }

    public void addTextButton(SelectPopEntity selectPopEntity,int position) {
        if (!adapter.getDatas().contains(selectPopEntity)) {
            adapter.addData(selectPopEntity,position);
        }
    }

    public int getTextButtonCount() {
        if (adapter.getDatas() != null) {
            return adapter.getDatas().size();
        }
        return 0;
    }

    public void removeTextButtonList(List<SelectPopEntity> list) {
        adapter.removeDatas(list);
    }


    public void removeTextButton(SelectPopEntity selectPopEntity) {
        adapter.removeItem(selectPopEntity);
    }
    public void removeTextButton(int position) {
        adapter.removeItem(position);
    }


    public void removeAllTextButton(){
        adapter.clearDatas();
    }


    public void contentSelect(boolean haveContent, boolean isOpen, int position) {
        adapter.getDatas().get(position).setCheck(haveContent);
        adapter.getDatas().get(position).setOpenPop(isOpen);
        adapter.notifyDataSetChanged();
    }

    public void contentSelect(String name,boolean haveContent, boolean isOpen, int position) {
        adapter.getDatas().get(position).setName(name);
        adapter.getDatas().get(position).setCheck(haveContent);
        adapter.getDatas().get(position).setOpenPop(isOpen);
        adapter.notifyDataSetChanged();
    }

    private void perseAttrs(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScreenTextView);
            //遍历AttributeSet中的XML属性
            //设置label名字
            /*tv_content.setText(typedArray.getString(R.styleable.ScreenTextView_tv_content));
            //设置颜色
            tv_content.setTextColor(typedArray.getColor(R.styleable.ScreenTextView_tv_color, getResources().getColor(R.color.color_333333)));*/
            typedArray.recycle();
        }
    }

    public interface ICallPop {
        void openPop(int position, TextView content, LinearLayout bg, SelectPopEntity entity);
    }

    public void onSetCallPop(ICallPop iCallPop) {
        this.iCallPop = iCallPop;
    }

    public String getName(int position){
        return adapter.getDatas().get(position).getName();
    }

    public int getSize(){
        return adapter.getItemCount();
    }
}